# import json
#
# import cv2
#
# Path = "/home/anmc/data/annotations/instances_train2014.json"
#
# with open(Path, 'r') as load_f:
#     load_dict = json.load(load_f)
#
# data = load_dict['annotations']
#
# dic = {}
#
# for id in data:
#
#     if id['image_id'] not in dic:
#         dic[id['image_id']] = []
#
#     dic[id['image_id']].append({"cls": id['category_id'], "bbox": id["bbox"]})
#
# print(dic)
#
# cls = load_dict['categories']
#
# for key in dic:
#
#     img = cv2.imread("/home/anmc/data/train2014/COCO_train2014_" + (len("000000000009") - len(str(key))) * "0" +
#                      str(key) + ".jpg")
#
#     print("\n\n_________________________________________")
#
#     for i in dic[key]:
#
#         for vval in cls:
#             if vval['id'] == i['cls']:
#                 name = vval['name']
#
#         cv2.putText(img, name, (int(i['bbox'][0]), int(i['bbox'][1])),
#                     cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
#         cv2.rectangle(img, (int(i['bbox'][0]), int(i['bbox'][1])), (int(i['bbox'][2] + i['bbox'][0]),
#                                                                     int(i['bbox'][3] + i['bbox'][1])), (255, 255, 255),
#                       2)
#
#     cv2.imshow("img", img)
#     cv2.waitKey(1000)

import cv2
import cocoapi

info = cocoapi.cocoAPI("/home/anmc/data/annotations/instances_train2014.json", "2014", "train", "person")
data = info.get_info()
for key in data:

    img = cv2.imread("/home/anmc/data/train2014/COCO_train2014_" + (len("000000000009") - len(str(key))) * "0" +
                     str(key) + ".jpg")

    print("\n\n_________________________________________")

    if len(data[key]) > 1:
        print(data[key])

    for i in data[key]:

        # for vval in cls:
        #     if vval['id'] == i['cls']:
        #         name = vval['name']

        cv2.putText(img, str(i['cls']), (int(i['bbox'][0]), int(i['bbox'][1])),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255))
        cv2.rectangle(img, (int(i['bbox'][0]), int(i['bbox'][1])), (int(i['bbox'][2] + i['bbox'][0]),
                                                                    int(i['bbox'][3] + i['bbox'][1])), (255, 255, 255),
                      2)

    cv2.imshow("img", img)
    cv2.waitKey(1000)
